﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF01
{
    class Program
    {
        static void Main(string[] args)
        {
            MyDbContext db = new MyDbContext();
            //db.Database.Log=log =>
            //{
            //    Console.WriteLine(log);
            //};

            //Student stu = new Student()
            //{
            //    Name = "王五",
            //    Sex = "男"
            //};
            //Console.WriteLine(db.Entry(stu).State);
            //db.Student.Add(stu);
            //Console.WriteLine(db.Entry(stu).State);
            //int result = db.SaveChanges();
            //if (result > 0)
            //{
            //    Console.WriteLine("ok");
            //}
            //Console.WriteLine(db.Entry(stu).State);
            //Console.ReadLine();

            //Student stu = new Student()
            //{
            //    Id = 1,
            //    Name = "张三",
            //    Sex = "女"
            //};
            //Console.WriteLine(db.Entry(stu).State);
            //db.Student.Attach(stu);
            //Console.WriteLine(db.Entry(stu).State);
            //db.Entry(stu).State = EntityState.Modified;
            //Console.WriteLine(db.Entry(stu).State);
            //int result = db.SaveChanges();
            //if (result > 0)
            //{
            //    Console.WriteLine("ok");

            //}
            //Console.WriteLine(db.Entry(stu).State);
            //Console.ReadLine();


            //Student stu = db.Student.Find(1);
            //stu.Name="李四";
            //int result = db.SaveChanges();
            //if (result > 0)
            //{
            //    Console.WriteLine("ok");
            //    Console.ReadLine();
            //}


            //Student stu = db.Student.Find(1);
            //db.Student.Remove(stu);
            //int result = db.SaveChanges();
            //if (result > 0)
            //{
            //    Console.WriteLine("ok");
            //    Console.ReadLine();
            //}

            //Student stu = new Student()
            //{
            //    Id = 2,

            //};
            //Console.WriteLine(db.Entry(stu).State);
            //db.Student.Attach(stu);
            //Console.WriteLine(db.Entry(stu).State);
            //db.Student.Remove(stu);
            //Console.WriteLine(db.Entry(stu).State);
            //int result = db.SaveChanges();
            //if (result > 0)
            //{
            //    Console.WriteLine("ok");
            //}
            //Console.WriteLine(db.Entry(stu).State);
            //Console.ReadLine();


            //List<Student> list = db.Student.ToList();
            //foreach (Student item in list)
            //{
            //    Console.WriteLine(item.Id + ":" + item.Name);
            //}
            //Console.ReadLine();
        } 
    }
}
